/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * @author Ashis Chettri
 */
public class FoafBean {

    private int personID;
    private String person;
    private String name;
    private String nickname;
    private String depiction;
    private List<MemberFriend> friendList;
    private String onlineAccount;
    private String accountName;
    private String accountServiceHomepage;
    private String seeAlso;
    private String tempPerson;
    private int boardID;

    public FoafBean() {
        friendList = new ArrayList<MemberFriend>();
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAccountServiceHomepage() {
        return accountServiceHomepage;
    }

    public void setAccountServiceHomepage(String accountServiceHomepage) {
        this.accountServiceHomepage = accountServiceHomepage;
    }

    public String getOnlineAccount() {
        return onlineAccount;
    }

    public void setOnlineAccount(String onlineAccount) {
        this.onlineAccount = onlineAccount;
    }

    public String getSeeAlso() {
        return seeAlso;
    }

    public void setSeeAlso(String seeAlso) {
        this.seeAlso = seeAlso;
    }

    public List<MemberFriend> getFriendList() {
        return friendList;
    }

    public String getDepiction() {
        return depiction;
    }

    public void setDepiction(String depiction) {
        this.depiction = depiction;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }

    public int getPersonID() {
        return personID;
    }

    public void setPersonID(int personID) {
        this.personID = personID;
    }

    public class MemberFriend {

        private int friendID;
        private int memberID;
        private String friend;
        private String friendNickname;
        private String friendseeAlso;

        public String getFriend() {
            return friend;
        }

        public void setFriend(String friend) {
            this.friend = friend;
        }

        public int getFriendID() {
            return friendID;
        }

        public void setFriendID(int friendID) {
            this.friendID = friendID;
        }

        public int getMemberID() {
            return memberID;
        }

        public void setMemberID(int memberID) {
            this.memberID = memberID;
        }

        public String getfriendNickname() {
            return friendNickname;
        }

        public void setfriendNickname(String friendNickname) {
            this.friendNickname = friendNickname;
        }

        public String getFriendseeAlso() {
            return friendseeAlso;
        }

        public void setFriendseeAlso(String friendseeAlso) {
            this.friendseeAlso = friendseeAlso;
        }
    }

    public void addMember() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String InsertMemberSql = "INSERT INTO tbl_member (`ID`, `Person`, `Name`, "
                + "`Nickname`, `Depiction`, `BoardID`) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            con = DBConnection.getDBConnection();
            con.setAutoCommit(false);
            tempPerson = getPerson();
            boardID = Integer.parseInt(tempPerson.substring(tempPerson.indexOf('=')+1,
                    tempPerson.indexOf('#')));
            ps = con.prepareStatement(InsertMemberSql);
            ps.setInt(1, getPersonID());
            ps.setString(2, tempPerson);
            ps.setString(3, getName());
            ps.setString(4, getNickname());
            ps.setString(5, getDepiction());
            ps.setInt(6, boardID);
            ps.executeUpdate();

            rs = con.prepareStatement("SELECT LAST_INSERT_ID()").executeQuery();
            if (rs.next()) {
                setPersonID(rs.getInt(1));
            }
            con.commit();
        } catch (SQLException ex) {
            try {
                con.rollback();// rollback on error
            } catch (SQLException ex1) {
            }
            System.err.println("Error while adding data to member...." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
    }

    public void addMemberAccount() {
        Connection con = null;
        PreparedStatement ps = null;
        String InsertMemberAccountSql = "INSERT INTO tbl_member_account"
                + "(`BoardID`, `OnlineAccount`, `AccountName`) "
                + "VALUES (?, ?, ?)";
        try {
            con = DBConnection.getDBConnection();
            ps = con.prepareStatement(InsertMemberAccountSql);
            ps.setInt(1, boardID);
            ps.setString(2, getOnlineAccount());
            ps.setString(3, getAccountName());
            ps.executeUpdate();
        } catch (SQLException ex) {
            System.err.println("Error while adding data to members account...." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException ex) {
            }
        }
    }

    public void addMemberFriend() {

        Connection con = null;
        PreparedStatement ps = null;
        String InsertMemberFriendSql = "INSERT INTO tbl_member_friend ("
                + "`BoardID`, `Friend`, `Nickname`, `FriendBoardID`) "
                + "VALUES (?, ?, ?, ?)";
        try {
            con = DBConnection.getDBConnection();
            ps = con.prepareStatement(InsertMemberFriendSql);
            for (MemberFriend friend : getFriendList()) {

                String tempFriend = friend.getFriend();
                ps.setInt(1, boardID);
                ps.setString(2, friend.getFriend());
                ps.setString(3, friend.getfriendNickname());
                ps.setInt(4, Integer.parseInt(tempFriend.substring(tempFriend.indexOf('=')+1,
                        tempFriend.indexOf('#'))));
                ps.executeUpdate();
            }
        } catch (SQLException ex) {
            System.err.println("Error while adding data to member friends...." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException ex) {
            }
        }
    }
}
